<style>
	.help{
		background-color: #f6f6f6;
		border: 1px solid #dedede;
		border-radius: 3px;
		line-height: 24px;
		margin-top: 20px;
		padding: 10px;
	}
</style>
<div class="divtable pd20">
	<button class="btn btn-success btn-sm" style="margin-bottom: 5px;" onclick="AddHook()">添加</button>
	<table class="table table-hover" width="100%" cellspacing="0" cellpadding="0" border="0">
		<thead>
			<tr>
				<th>名称</th>
				<th>添加时间</th>
				<th>密钥</th>
              	<th>接口</th>
              	<th>Git</th>
				<th>目录</th>
				<th style="text-align: right;">操作</th>
			</tr>
		</thead>
		<tbody id="zipBody"></tbody>
	</table>
</div>
<script type="javascript/text">
	//添加
	function AddHook(act){
		if(act == 1){
			var loadT = layer.msg("提交中...",{icon:16,time:0});
			var data = $('#f').serializeArray();
			$.post("/plugin?action=a&s=AddHook&name=gyxz",data,function(rdata){
				$(".closeWebHook").click();
				layer.close(loadT);
				layer.msg(rdata.msg,{icon:rdata.status?1:2});
				if(rdata.status) GetHookList();
			})
			return;
		}else{
        	$.post("/plugin?action=a&s=web&name=gyxz",data,function(rdata){
				list=rdata.html;
			});
        }
		var index = layer.open({
			type: 1,
			area: '600px',
			title: '添加Hook',
			closeBtn: 2,
			shift: 5,
			shadeClose: false,
			content: "<form id='f'><div class='bt-form pd20 pb70'>\
						<div class='line'>\
						<span class='tname'>名称</span>\
						<div class='info-r'><input class='bt-input-text' type='text' placeholder='显示的标题' id='hook_title' name='title' style='width:340px' /></div>\
						</div>\
						<div class='line'>\
						<span class='tname'>Git地址</span>\
						<div class='info-r'><input class='bt-input-text' type='text' id='git' name='git' placeholder='Git地址，请输入SSH' style='width:340px' /></br>请输入SSH地址</div>\
						</div>\
                        <div class='line'>\
						<span class='tname'>同步目录</span>\
						<div class='info-r'><select class='bt-input-text mr5 dirName' name='url' style='width:50%'>"+list+"</select></div>\
						</div>\
                        <div class='line'>\
						<span class='tname'>初始化</span>\
						<div class='info-r'><input class='btswitch btswitch-ios' onclick='$(\"#hide\").toggle();' id='init' name='init' type='checkbox'><label class='btswitch-btn phpmyadmin-btn' for='init'></label>*设置账户邮箱</div>\
						</div>\
                        <div id='hide' hidden>\
                        <div class='line'>\
                        <span class='tname'>账户</span>\
						<div class='info-r'><input class='bt-input-text' type='text' id='user' name='user' placeholder='码云账户' style='width:340px' /></div>\
						</div>\
                        <div class='line'>\
                        <span class='tname'>邮箱</span>\
						<div class='info-r'><input class='bt-input-text' type='text' id='email' name='email' placeholder='码云邮箱' style='width:340px' /></div>\
						</div>\
                        <div class='line'>\
						<span class='tname'>生成SSH</span>\
						<div class='info-r'><input class='btswitch btswitch-ios' id='ssh_init' name='ssh_init' type='checkbox'><label class='btswitch-btn phpmyadmin-btn' for='ssh_init'></label></div>\
						</div>\
                        <div class='line'>\
						<span class='tname'>上传SSH</span>\
						<div class='info-r'><input class='btswitch btswitch-ios' onclick='$(\"#upda\").toggle();' id='update' name='update' type='checkbox'><label class='btswitch-btn phpmyadmin-btn' for='update'></label></br>*上传SSH至码云</div>\
						</div>\
                        <div id='upda' hidden>\
                        <div class='line'>\
                        <span class='tname'>Git Token</span>\
						<div class='info-r'><input class='bt-input-text' type='text' id='token' placeholder='码云/设置/私人令牌/新建Token' name='token' style='width:340px' /></br></div>\
						</div>\
                        <div class='line'>\
                        <span class='tname'>Git 标题</span>\
						<div class='info-r'><input class='bt-input-text' type='text' id='ssh_title' placeholder='码云SSH里显示的公钥标题，默认为IP' name='ssh_title' style='width:340px' /></div>\
						</div>\
                        </div>\
                        </div>\
						<div class='bt-form-submit-btn'>\
							<button type='button' class='btn btn-danger btn-sm closeWebHook' >关闭</button>\
					        <button type='button' class='btn btn-success btn-sm' onclick=\"AddHook(1)\" >提交</button>\
				        </div>\
				      </div></form>"
		});
		$(".closeWebHook").click(function(){
			layer.close(index);
		})
	}
	//获取列表
	function GetHookList(){
		var loadT = layer.msg('正在获取列表...',{icon:16,time:0,shade: [0.3, '#000']});
		$.get('/plugin?action=a&name=gyxz&s=GetListA',function(mlist){
			layer.close(loadT);
			var zbody = '';
			for(var i=0;i<mlist.length;i++){
				zbody += '<tr>'
							+'<td>'+mlist[i].title.replace('git','')+'</td>'
							+'<td>'+getLocalTime(mlist[i].addtime)+'</td>'
							+'<td><a href="javascript:showWebHookCode(\''+mlist[i].access_key+'\',\'秘钥\')" class="btlink">查看密钥</a></td>'
                            +'<td><a href="javascript:showWebHookCode(\''+mlist[i].access_key+'\',\'url\')" class="btlink">查看接口</a></td>'
							+'<td><a href="javascript:showWebHookCode(\''+mlist[i].git+'\',\'Git\')" class="btlink">查看Git</a></td>'
                            +'<td>'+mlist[i].url+'</td>'
							+'<td><a href="javascript:DeleteHook(\''+mlist[i].access_key+'\');" class="btlink">删除</a></td>'
						+'</tr>'
			}
			$("#zipBody").html(zbody);
		});
	}
	//查看密钥
	function showWebHookCode(code,title){
    	if(title=='url'){
        	$.get("/plugin?action=a&s=url&name=gyxz",function(rdata){
				code="http://"+rdata.url+"/hook?access_key="+code;
                layer.open({
                  type:1,
                  title:'查看'+title,
                  area: '510px', 
                  shadeClose:false,
                  closeBtn:2,
                  content:'<div class="bt-form pd20">\
                          <div class="line"><span class="tname" style="width:50px">'+title+'</span><input class="bt-input-text" value="'+code+'" style="width:420px"></div>\
                          </div>'	
              });
			});
        }else{
        	layer.open({
                type:1,
                title:'查看'+title,
                area: '510px', 
                shadeClose:false,
                closeBtn:2,
                content:'<div class="bt-form pd20">\
                        <div class="line"><span class="tname" style="width:50px">'+title+'</span><input class="bt-input-text" value="'+code+'" style="width:420px"></div>\
                        </div>'	
            });
        }
		
	}
	//删除
	function DeleteHook(key){
		$.post("/plugin?action=a&s=DelHook&name=gyxz","access_key="+key,function(rdata){
			layer.msg(rdata.msg,{icon:rdata.status?1:2});
			GetHookList();
		});
	}
	GetHookList();
</script>